package com.automobile.controller;

import com.automobile.domain.Img;
import com.automobile.domain.Smart;
import com.automobile.domain.User;
import com.automobile.service.CollectService;
import com.automobile.service.ImgService;
import com.automobile.service.SmartService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 类的作用
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-03-25 18:53
 */
@Controller
@RequestMapping("/smart")
public class SmartController {

    @Autowired
    private SmartService smartService;

    @Autowired
    private ImgService imgService;

    @Autowired
    private CollectService collectService;

    @RequestMapping("/findAll")
    public @ResponseBody
    PageInfo findAll(@RequestParam(value = "page",required = true,defaultValue = "1")Integer page,
                     @RequestParam(value = "size",required = true,defaultValue = "12")Integer size) throws Exception {
        PageInfo pageInfo = null;
        List<Smart> smarts = smartService.findAll(page, size);
        pageInfo = new PageInfo(smarts);
        System.out.println("pageInfo = =========  " + pageInfo);
        return pageInfo;
    }

    @RequestMapping("/query")
    public @ResponseBody PageInfo query(@RequestParam("title") String title,@RequestParam("page")int page){

        List<Smart> smarts = null;
        try {
            smarts = smartService.query(title, page, 12);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageInfo pageInfo = new PageInfo(smarts);
        return pageInfo;
    }

    @RequestMapping("/del")
    public @ResponseBody int del(@RequestBody List<Integer> ids){
        try {
            smartService.delete(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 1;
    }


    @RequestMapping("/message")
    public @ResponseBody Smart message(@RequestBody Integer id){
        Smart smart = smartService.findById(id);
        return smart;
    }


    @RequestMapping("/findCollect")
    public @ResponseBody int findCollect(@RequestBody int id){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(user == null){
            return -1;
        }
        return collectService.findCollect2(user.getId(), id);
    }

    @RequestMapping("/taggleCollect")
    public @ResponseBody int deleteCollect(@RequestBody int id){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if(user == null){
            return -1;
        }
        return collectService.taggleSmart(user.getId(),id);
    }

}
